/**
 * 
 */
package com.base.myproject.client.busi.b2c.analysis;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.QueryParamPanel;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.LineDataProvider;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.LineChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;

/**
 * 客户睡眠度分析，通过用户登录，购买产品的时间判断客户的睡眠度
 * 
 * @author zgw 2009-8-26 上午10:33:19 javazgw@gmail.com
 */
public class CcodeSleepAnlysis extends ContentPanel {

	BaseStore<JDBCModelData> store;
	
	SelectionListener<ButtonEvent> listener ;
	ComboBox<BaseModelData> combo2 ;
	public CcodeSleepAnlysis() {
		init();
	}

	void init() {
		setWidth("400px");
		initlistener();
		initstore();
		initQueryParamPanel();
		initWidget();
	}

	void initstore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("ccode");
		// type.addField("orderdate");
		// type.addField("cname");
		type.addField("ym");
		type.addField("listnum");
		type.addField("loginnum");
		// type.addField("totle");

		StoreInfo info = new StoreInfo();

		// info.setKeyname(new String[] { "b2cgcode" });
		info.setTablename("sleeping");
		// info.setSql("");
		store = new BaseStore<JDBCModelData>(type, info,false);

	}

	void initWidget() {
		ChartModel model = new ChartModel("用户睡眠度分析",
				"font-size: 16px; font-family: Verdana; text-align: center;");
		model.setBackgroundColour("#fefefe");
		model.setLegend(new Legend(Position.TOP, true));
		model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER);

		
		  BarChart bar = new BarChart(BarStyle.GLASS);
		  bar.setColour("#00aa00"); 
		  BarDataProvider barProvider = new	  BarDataProvider( "listnum","ym");
		  barProvider.bind(store);
		 bar.setDataProvider(barProvider); 
		 //bar.addChartListener(listener);
		 model.addChartConfig(bar);
		 
		  BarChart loginbar = new BarChart(BarStyle.GLASS);
		  loginbar.setColour("#ff6600"); 
		  BarDataProvider loginbarProvider = new	  BarDataProvider( "loginnum","ym");
		  loginbarProvider.bind(store);
		  loginbar.setDataProvider(loginbarProvider); 
		 //bar.addChartListener(listener);
		 model.addChartConfig(loginbar);
		 

		LineChart line = new LineChart();
		line.setAnimateOnShow(true);
		line.setText("购物次数");
		line.setColour("#00aa00");
		LineDataProvider lineProvider = new LineDataProvider("listnum", "ym");
		lineProvider.bind(store);
		line.setDataProvider(lineProvider);
		model.addChartConfig(line);

		LineChart loginline = new LineChart();
		loginline.setAnimateOnShow(true);
		loginline.setText("登录次数");
		loginline.setColour("#ff6600");
		LineDataProvider loginlinelineProvider = new LineDataProvider(
				"loginnum", "ym");
		loginlinelineProvider.bind(store);
		loginline.setDataProvider(loginlinelineProvider);
		model.addChartConfig(loginline);
		String url = "chart/open-flash-chart.swf";
		final Chart chart = new Chart(url);
		// chart.setToolTip("点击产品的图型看看");
		chart.setChartModel(model);
		chart.setHeight("300px");
		add(chart);
	}

	void initQueryParamPanel() {
		ContentPanel parampanel = new ContentPanel();
		//FormPanel fp = new FormPanel();
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("ccode");
		// type.addField("orderdate");
		// type.addField("cname");
		type.addField("cname");
		// type.addField("totle");

		StoreInfo info = new StoreInfo();

		info.setTablename("ccode");
		info.setNeedAllRow("no");
		info.setForcenolimit("true");
		 info.setSql("select ccode,cname from ccode");
		BaseStore<BaseModelData> ccodestore = new BaseStore<BaseModelData>(
				type, info);		
		ccodestore.setLoader(null);
		combo2 = new ComboBox<BaseModelData>();
		combo2.setWidth(150);
//		ccodestore.getLoader()
		combo2.setStore(ccodestore);
		
		combo2.setDisplayField("cname");
		combo2.setTypeAhead(true);
		///combo2.setTypeAheadDelay(100);
		combo2.setTriggerAction(TriggerAction.ALL);
		combo2.setAutoValidate(true);
//		combo2.set
		

		QueryParamPanel qpp = new QueryParamPanel(2);
		qpp.addWidget("客户名",combo2);
		
		qpp.addQueryButton(listener);
		add(qpp);
	}
	
	 void initlistener()
	 {
		 listener = new SelectionListener<ButtonEvent>()
		 {
				public void componentSelected(ButtonEvent ce) {
					// TODO Auto-generated method stub
					if(ce.getButton().getText().equals("查询"))
					{
						
						StoreInfo info = store.getStoreinfo();
						String str = "";
						str += "select b.ym,b.ccode,sum(isnull(a.countnum,0)) as listnum,sum(isnull(b.countnum,0)) as loginnum from ";
						str += "( ";
						str += "select ccode ,cast(year(orderdate) as varchar(4))%2B'-'%2Bcast(month(orderdate) as varchar(4)) as ym,count(*) as countnum from b2clistorder group by ccode,cast(year(orderdate) as varchar(4))%2B'-'%2Bcast(month(orderdate) as varchar(4)) ";
						str += ") as a ";
						str += "right join ";
						str += "( ";
						str += "select ccode ,cast(year(logindate) as varchar(4))%2B'-'%2Bcast(month(logindate) as varchar(4)) as ym,count(*) as countnum from b2clogininfo group by ccode,cast(year(logindate) as varchar(4))%2B'-'%2Bcast(month(logindate) as varchar(4)) ";
						str += ") as b ";
						str += "on a.ccode=b.ccode and a.ym= b.ym ";
						str += " where b.ccode ='"+combo2.getValue().get("ccode")+"'";
						str += "			group by b.ccode,b.ym ";
						info.setSql(str);
						store.reload();
						
						System.out.println(combo2.getValue().get("ccode"));
					}
				}
		 };
		 
	 }
}
